package cn.tedu.tmall.passport.controller;


import cn.tedu.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.common.web.JsonResult;
import cn.tedu.tmall.passport.pojo.param.LoginByPhoneParam;
import cn.tedu.tmall.passport.pojo.param.UserLoginInfoParam;
import cn.tedu.tmall.passport.pojo.param.UserRegParam;
import cn.tedu.tmall.passport.pojo.vo.UserLoginResultVO;
import cn.tedu.tmall.passport.service.IUserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/passport")
@Api(tags = "单点登录")
public class UserController {

    @Autowired
    private IUserService userService;

    //用户注册
    @PostMapping("/users/reg")
    @ApiOperation("用户注册")
    public JsonResult userReg( UserRegParam userRegDTO){
        System.out.println(".........................................."+userRegDTO);
        userService.userReg(userRegDTO);
        return JsonResult.ok();
    }

    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 100)
    @PostMapping("/login")
    public JsonResult login(@Valid UserLoginInfoParam userLoginInfoParam,
                            @ApiIgnore HttpServletRequest request) {
        System.out.println("userLoginInfoParam >>> " + userLoginInfoParam);
        String remoteAddr = request.getRemoteAddr();
        String userAgent = request.getHeader("User-Agent");
        UserLoginResultVO loginResult
                = userService.login(userLoginInfoParam, remoteAddr, userAgent);
        return JsonResult.ok(loginResult);
    }

    @ApiOperation("退出登录")
    @ApiOperationSupport(order = 200)
    @GetMapping("/logout")
    public JsonResult logout(@ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal) {
        userService.logout(currentPrincipal);
        return JsonResult.ok();
    }


    @ApiOperation("发送验证码")
    @ApiOperationSupport(order = 110)
    @GetMapping("/sendMessage/{phone}")
    public JsonResult sendMessage(@PathVariable String phone) {
        System.out.println("phone >>> " + phone);
        userService.sendMessage(phone);
        return JsonResult.ok();
    }

    @ApiOperation("通过验证码登录")
    @ApiOperationSupport(order = 120)
    @PostMapping("/loginByCode")
    public JsonResult sendMessage(@Valid LoginByPhoneParam loginByPhoneParam,
                                  @ApiIgnore HttpServletRequest request) {

        System.out.println("loginByPhoneParam >>> " + loginByPhoneParam);
        String remoteAddr = request.getRemoteAddr();
        String userAgent = request.getHeader("User-Agent");

        UserLoginResultVO loginResult = userService.loginByPhone(loginByPhoneParam, remoteAddr,userAgent);
        return JsonResult.ok(loginResult);
    }

}
